package Dao.imp;

import entity.Collect;
import mapper.CollectMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import utils.SqlSessionFactoryUtils;

import java.util.List;

public class CollectDao implements CollectMapper {
    private SqlSessionFactory sqlSessionFactory;

    public CollectDao() {
        sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
    }

    @Override
    public int addCollect(Collect collect) {
        int result = -1;
        try (SqlSession session = sqlSessionFactory.openSession()) {
            result = session.insert("addCollect", collect);
            session.commit();
        }
        return result;
    }

    @Override
    public int deleteCollect(int cid) {
        int result = -1;
        try (SqlSession session = sqlSessionFactory.openSession()) {
            result = session.delete("deleteCollect", cid);
            session.commit();
        }
        return result;
    }

    @Override
    public List<Collect> queryCollectByUid(int uid) {
        List<Collect> list = null;
        try (SqlSession session = sqlSessionFactory.openSession()) {
            list = session.selectList("queryCollectByUid", uid);
        }
        return list;
    }
}
